<?php
namespace Modules\Activity\Http\Controllers\Admin\YiFanShang;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\YiFanShang\Room;
use \Modules\Activity\Entities\YiFanShang\Activity;

use Validator;
use ThrowException;
use Auth;
use DB;

// 一番赏活动
class RoomController extends Controller
{
    // 房间列表
    public function index(Request $req, $uuid)
    {
        $options = $req->all();

        $options['per_page'] = $req->input('per_page', 10);
        
        $activity = Activity::getEntity([
        	'uuid' => $uuid
        ]);

        $options['builder'] = $activity->rooms()->orderBy('id', 'asc');

        $options['only'] = [
        	'id',
        	'uuid',
        	'gift_skus',
        	'normal_skus',
        	'stock',
            'open_records',
        	'num'
        ];

        $skus = $activity->skus;

        $list = Room::getList($options);

        $list->each(function ($room) use ($skus) {

        	$normalSkuList = [];
        	$giftSkuList = [];

        	foreach ($skus as $sku) {
        		$info = [
        			'id' => $sku->id,
        			'shang_title' => $sku->shang_title,
        			'stock' => $room->calcStockForSku($sku),
        			'total' => $sku->total_per_room,
        		];

        		if ($sku->shang_type === 1) {
        			array_push($giftSkuList, $info);
        		}
        		else {
        			array_push($normalSkuList, $info);
        		}
        	}

        	$room->normal_skus = $normalSkuList;
        	$room->gift_skus = $giftSkuList;

        });

        return $list->generateListResponse();
    }
}
